using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameStateManagement;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Content;

namespace GameStateManagementSample.QuickMath
{
    class CircleMenu
    {
        public Vector2 Center;
        public float Radius;
        public double Angle;

        Texture2D m_Texture;
        Texture2D m_TexButtonOK;
        SpriteBatch m_SpriteBatch;
        SpriteFont m_Font;
        ContentManager content;
        public double AngleOld;
        public bool isRotation;
        int idTouch;
        Vector2 posTouchOld;

        public CircleMenu()
        {
            Center = Vector2.Zero;
            Radius = 0;
            Angle = 0;
        }
        public CircleMenu(float _CenterX, float _CenterY, float _radius, ScreenManager _screenmanger)
        {
            Center = new Vector2(_CenterX, _CenterY);
            Radius = _radius;
            Angle  = 0;
            
            m_SpriteBatch = new SpriteBatch(_screenmanger.Game.GraphicsDevice);
            
            content = new ContentManager(_screenmanger.Game.Services, "Content");
            m_Font = content.Load<SpriteFont>("gamefont");
            m_Texture = content.Load<Texture2D>(@"Image/circlemenu");
            m_TexButtonOK = content.Load<Texture2D>(@"Image/buttonOK");
        }
        public bool Intersect(float _x, float _y)
        {
            double temp = (_x - Center.X) * (_x - Center.X) + (_y - Center.Y) * (_y - Center.Y);

            return (temp < (Radius * Radius));
        }
        public void InputTouch(TouchCollection _touches)
        {
            foreach (TouchLocation touch in _touches)
            {
                Vector2 vDistan = touch.Position - Center;
                /* if (vDistan.Length() < 49) continue;*/
                if (touch.State == TouchLocationState.Pressed && isRotation == false && vDistan.Length() >= 49)
                {
                    if (Intersect(touch.Position.X, touch.Position.Y))
                    {
                        AngleOld = Angle;
                        isRotation = true;
                        idTouch = touch.Id;
                        posTouchOld = touch.Position;
                    }
                }

                if (touch.State == TouchLocationState.Moved)
                {
                    if (touch.Id == idTouch && isRotation == true)
                    {

                        if (Intersect(touch.Position.X, touch.Position.Y) && vDistan.Length() > 49)
                        {

                            Vector2 Vold = posTouchOld - Center;
                            Vector2 Vnew = touch.Position - Center;
                            posTouchOld = touch.Position;
                            double temp = (Vold.X * Vnew.X + Vold.Y * Vnew.Y) /
                                (float)(Vold.Length() * Vnew.Length());
                            if (temp > 1) temp = 1;
                            if (temp < -1) temp = -1;

                            double a = 0;
                            a = Math.Acos(temp);

                            double Aold = Math.Atan(Vold.Y / Vold.X);
                            double Anew = Math.Atan(Vnew.Y / Vnew.X);
                            if (Aold < 0)
                            {
                                Aold += Math.PI * 2;
                            }
                            if (Anew < 0)
                            {
                                Anew += Math.PI * 2;
                            }
                            if (Anew - Aold > 0)
                            {
                                if (Anew - Aold > Math.PI * 1.75f)
                                    Angle -= a;
                                else Angle += a;
                            }
                            else
                            {
                                if (Anew - Aold < -Math.PI * 1.75f)
                                    Angle += a;
                                else Angle -= a;
                            }

                            //Angle += a;       

                            if (Angle > Math.PI * 2) Angle = 0;
                            if (Angle < 0) Angle = Math.PI * 2;

                        }
                        else
                        {
                            isRotation = false;
                        }
                    }
                }
                if (touch.State == TouchLocationState.Released && touch.Id == idTouch)
                {
                    isRotation = false;
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            if (!isRotation)
            {
                double temp = (int)((Angle + Math.PI * 0.25) / (float)(Math.PI * 0.5f));
                temp *= Math.PI * 0.5f;
                if (Angle < temp)
                {
                    Angle += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (Angle > temp) Angle = temp;
                }
                if (Angle > temp)
                {
                    Angle -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (Angle < temp) Angle = temp;
                }
            }
        }
        public void Draw()
        {
            Matrix matrix;
            matrix = Matrix.Identity * Matrix.CreateTranslation(-200, -200, 0) *
                Matrix.CreateRotationZ((float)Angle) *
                Matrix.CreateTranslation(600, 280, 0) *
                Matrix.CreateScale(1.0f, 1.0f, 1.0f);


            m_SpriteBatch.Begin(SpriteSortMode.BackToFront,
                BlendState.AlphaBlend,
                null, null, null, null, matrix);

            m_SpriteBatch.Draw(m_Texture, new Vector2(0, 0), null, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

            m_SpriteBatch.End();

            m_SpriteBatch.Begin();
            m_SpriteBatch.Draw(m_TexButtonOK, new Vector2(Center.X - 60, Center.Y - 60), Color.White);
            m_SpriteBatch.End();
        }

        public int Select()
        {
            int select = (int)((Angle + Math.PI + Math.PI * 0.25f) / (Math.PI * 0.5));
            select = select % 4;
            /*select = 8 - select;*/
            if (select == 4) select = 0;

            return select;
        }
    }
    
}
